package chzhenjia.learn.androidcode.sms;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

import chzhenjia.learn.androidcode.R;

/**
 * 发送短信的 activity
 *
 * @author 陈圳佳
 *         日期:2015/7/13
 */
public class SmsActivity extends AppCompatActivity {

    private TextView etPhone;
    private TextView etContent;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sms_btn_send:
                    sendSms();
                    break;
            }
        }
    };

    /**
     * 发送短信
     */
    private void sendSms() {
        String phone = etPhone.getText().toString();
        String content = etContent.getText().toString();
//      调用发送短信的 api
        SmsManager smsManager = SmsManager.getDefault();
//      如果短信内容过长会发送不出去,所以需要分割短信内容
        ArrayList<String> list = smsManager.divideMessage(content);
        for (String s : list) {
            smsManager.sendTextMessage(phone, null, s, null, null);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);
        etPhone = (TextView) findViewById(R.id.sms_et_phone);
        etContent = (TextView) findViewById(R.id.sms_et_content);
        Button btnSend = (Button) findViewById(R.id.sms_btn_send);
        btnSend.setOnClickListener(onClickListener);
    }

}
